using System;
using System.IO;
using Logging;
using System.Diagnostics;

namespace NyMPH.Persistence
{
    /// <summary>
    /// This class provides access to the filesystem for business objects. It can read/write files to disk.
    /// This also provides lists of files in a path and can also 'watch' a file for changes.
    /// </summary>
    public class FileSystem
    {
        //from design, forgot what this is for :)
        protected string Locations;

        //Create a file log for errors just for debugging, otherwise write to event log
#if DEBUG
        private static EventLogHelper _elh = new EventLogHelper("NyMPH.Persistence.FileSystem.Errors.log");

#else
        private EventLogHelper _elh = new EventLogHelper("NyMPH", "FileSystem");
         
#endif

        #region Methods

        /// <summary>
        /// This method writes the Data to the Location. 
        /// </summary>
        /// <param name="Location">The path and filename to write data to.</param>
        /// <param name="Data">The data to write to the file.</param>
        /// <returns>True if write operation succeeded, otherwise false.</returns>
        public static bool Write(string Location, string Data)
        {
            try
            {
                File.WriteAllText(Location, Data);
            }
            catch (Exception ex)
            {
                _elh.WriteEvent(string.Format("Exception occurred while writing to {0}. Exception details: {1}", Location, ex.Message), EventLogEntryType.Error);
            }

            return File.Exists(Location);
        }

        /// <summary>
        /// This method reads in the file specified by the Location parameter.
        /// If an exception occurs, a null string is returned.
        /// </summary>
        /// <param name="Location">The path and filename of the file to read in.</param>
        /// <returns>The contents of the file as a single string. This will be null if the file doesn't exist or an exception occurs.</returns>
        public static string Read(string Location)
        {
            try
            {
                //return contents of file as string if it exists, otherwise null
                return File.Exists(Location) ? File.ReadAllText(Location) : null;
            }
            catch (Exception ex)
            {
                _elh.WriteEvent(string.Format("Exception occurred while reading {0}. Exception details: {1}", Location, ex.Message), EventLogEntryType.Error);
                return null;
            }

        }

        /// <summary>
        /// This method deletes a file. There are no confirmation checks; the UI should handle this.
        /// </summary>
        /// <param name="Location">The file to delete.</param>
        /// <returns></returns>
        public static bool Delete(string Location)
        {
            //todo: implement better error handling
            File.Delete(Location);

            return true;
        }

        public void WatchFile(string Location, object IsModifiedDelegate)
        {
            // TODO add implementation
        }

        /// <summary>
        /// Returns an array of file paths (as string) for the Path specified. This search can be filtered using normal search pattern strings (i.e. *.pch).
        /// </summary>
        /// <param name="Path">The directory to list files from.</param>
        /// <param name="Filter">A search pattern string to filter the file list, such as "*.pch" or "piano*.*". If this pattern is null or empty it will be treated as "*.*" (everything).</param>
        /// <returns>An array of strings describing the full path of each file in the given Path.</returns>
        public static string[] ListFiles(string Path, string Filter)
        {
            //return all files if search filter is null
            if (string.IsNullOrEmpty(Filter))
            {
                Filter = "*.*";
            }

            try
            {
                if (Directory.Exists(Path))
                {
                    return Directory.GetFiles(Path, Filter, SearchOption.TopDirectoryOnly);
                }
                else
                //TODO: FIX THIS
                { return null; }
            }
            catch (Exception ex)
            {
                _elh.WriteEvent(string.Format("Exception occurred while listing files from directory {0}. Exception details: {1}", Path, ex.Message), EventLogEntryType.Error);
                return null;
            }
        }

        /// <summary>
        /// Returns an array of file paths (as string) for the Path specified. All files will be returned.
        /// </summary>
        /// <param name="Path">The directory to list files from.</param>
        /// <returns>An array of strings describing the full path of each file in the given Path.</returns>
        public static string[] ListFiles(string Path)
        {
            return ListFiles(Path, "*.*");
        }

        #endregion



    }
}